package top.catchcat.blogs.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.catchcat.blogs.dao.blogs.Blog;
import top.catchcat.blogs.dao.blogs.BlogQuery;
import top.catchcat.blogs.enums.SortEnum;
import top.catchcat.blogs.mapper.BlogMapper;
import top.catchcat.blogs.service.BlogService;
import top.catchcat.blogs.utils.MarkdownUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;

    @Override
    public boolean insertOrUpdateBlog(Blog blog) {
        if (blog.getId() == null)
            return blogMapper.insertBlog(blog);
        else
            return blogMapper.updateBlog(blog);
    }

    @Override
    public boolean deleteBlog(Long id) {
        if (id != null) return blogMapper.deleteBlog(id);
        else return false;
    }

    @Override
    public Blog getBlogById(Long id, boolean isOpened) {
        if (id != null) {
            Blog blogById = blogMapper.getBlogById(id);
            if (isOpened) blogById.setContent(MarkdownUtils.markdownToHtmlExtensions(blogById.getContent()));
            return blogById;
        }
        else return new Blog();
    }

    @Override
    public PageInfo<Blog> getAllBlogs(String search, String tag, String sort, Integer pageNum, Integer pageSize) {
        if (StringUtils.isNotBlank(search)) {
            String[] tags = StringUtils.split(search, " ");
            search = Arrays.
                    stream(tags)
                    .filter(StringUtils::isNotBlank)
                    .map(t -> t.replace("+", "").replace("*", "").replace("?", ""))
                    .filter(StringUtils::isNotBlank).
                            collect(Collectors.joining("|"));
        }

        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setSearch(search);
        //防止+*?引起搜索异常
        if(StringUtils.isNotBlank(tag)){
            tag = tag.replace("+", "").replace("*", "").replace("?", "");
            blogQuery.setTags(tag);
        }
        for (SortEnum sortEnum : SortEnum.values()) {
            if (sortEnum.name().toLowerCase().equals(sort)) {
                blogQuery.setSort(sort);
                if (sortEnum == SortEnum.HOT7) {
                    blogQuery.setTime(new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 7));
                }
                if (sortEnum == SortEnum.HOT30) {
                    blogQuery.setTime(new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30));
                }
                break;
            }
        }

        PageHelper.startPage(pageNum, pageSize);
        List<Blog> blogs = blogMapper.getAllBlogsBySearch(blogQuery);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogs);
        return pageInfo;
    }

    @Override
    public PageInfo<Blog> getAllBlogsByUserId(Integer pageNum, Integer pageSize, Long userId) {
        PageHelper.startPage(pageNum, pageSize, "updateTime desc");
        List<Blog> blogs = blogMapper.getAllBlogsByUserId(userId);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogs);
        return pageInfo;
    }
}
